#!/bin/bash 
echo "正在检查系统是否更新..."
if [ $? -eq 0 ]; then
    echo "系统已更新"
else
    apt update && apt upgrade -y
    echo "开始更新"
fi

if grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
    echo "系统为ubuntu"
fi

if [ "$(whoami)" != "root" ]; then
    sudo -i
fi
echo "您已经通过Ubuntu系统和root权限的检查，可以运行其他命令了"
function download_and_install { 
    local package_name=$1 
    local download_command=$2 
    local install_command=$3 

    if ! [ -x "$(command -v $package_name)" ]; then 
        echo "正在下载 $package_name" 
        eval $download_command 
        echo "$package_name 下载成功" 

        echo "正在安装 $package_name" 
        eval $install_command 
        echo "$package_name 安装成功" 
    fi 
} 

download_and_install "redis" "apt install -y redis" "redis-server --daemonize yes --save 900 1 --save 300 10" 
download_and_install "git" "apt install -y git" "" 
download_and_install "fonts-wqy-microhei" "apt install -y --force-yes --no-install-recommends fonts-wqy-microhei" "" 
download_and_install "ttf-wqy-zenhei" "apt install -y --force-yes --no-install-recommends ttf-wqy-zenhei" "" 
download_and_install "node" "curl -sL https://deb.nodesource.com/setup_18.x | bash -" "apt-get install -y nodejs" 

if ! [ -x "$(command -v pnpm)" ]; then
    npm install -g pnpm --registry=https://registry.npm.taobao.org
    cd ~
fi

if ! [ -x "$(command -v chromium)" ]; then
    echo "正在下载 chromium"
    git clone https://gitee.com/Ganyu256/chromium.git --depth=1
fi

echo "----------安装云崽---------"
echo "    1.安装 Miao-Yunzai "
echo "    2.安装 Yunzai-Bot  "
echo "    3.安装 yxybot      "
echo "---------------------------"
read -p "请选择你要安装的云崽: " var

case $var in
    1)
        echo "开始安装 Miao-Yunzai"
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git
        cd Miao-Yunzai
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        pnpm install image-size -w
        pnpm install -P
        pnpm i
        cd $HOME
        ;;
    2)
        echo "开始安装 Yunzai-Bot"
        git clone --depth=1 https://gitee.com/Le-niao/Yunzai-Bot.git
        cd Yunzai-Bot                 
        git remote set-url origin https://gitee.com/yoimiya-kokomi/Yunzai-Bot.git && git checkout main && git pull
        git reset --hard origin/main
        pnpm install -P
        pnpm install pm2 -g
        pm2 update
        pnpm i
        cd $HOME
        ;;
    3) 
        echo "开始安装 云溪院月崽 "
        git clone --depth=1 -b v1 https://gitee.com/yunxiyuan/yxybot
        pnpm install -P
        pnpm i
        ;;
    *)
        echo "由于笨比的你乱选，开始自动下载miao-yunzai"
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git
        cd Miao-Yunzai
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        pnpm install image-size -w
        pnpm install -P
        pnpm i
        cd $HOME
        ;;
esac

if ! [ -x "$(command -v chromium)" ]; then
    echo "开始解码chrome与云崽的适配"
    cd chromium
    bash install.sh
    echo "chromium 配置成功"
    cd ..
fi

if [ -d "/root/Miao-Yunzai" ]; then
    LS_DIR='/root/Miao-Yunzai'
elif [ -d "/root/Yunzai-Bot" ]; then
    LS_DIR='/root/Yunzai-Bot'
elif [ -d "/root/yxybot" ]; then
    LS_DIR='/root/yxybot'
else
    echo "您未安装机器人"
    exit 1
fi
cd $HOME
XRK_DIR='/root/xrk'
PLUGINS_DIR="$LS_DIR/plugins"

if ! [ -d "$PLUGINS_DIR/xiaoyue-plugin" ]; then
    cd $LS_DIR
    git clone --depth=1 https://gitee.com/yunxiyuan/xiaoyue-plugin.git ./plugins/xiaoyue-plugin
    cd ~
fi
if ! grep -q "cat .ml" "$HOME/.profile"; then
    sed -i '/cat .yzml/d' "$HOME/.profile"
    sed -i '9a cat .ml' "$HOME/.profile"
    sed -i '10!s/cat .ml//' "$HOME/.profile"
fi
YZ_SCRIPT="/usr/bin/yz"
if [ ! -f "$YZ_SCRIPT" ]; then
    echo '#!/bin/bash' > "$YZ_SCRIPT"
    echo 'echo 正在启动Yunzai-Bot' >> "$YZ_SCRIPT"
    echo "cd $LS_DIR" >> "$YZ_SCRIPT"
    echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$YZ_SCRIPT"
    echo 'node app' >> "$YZ_SCRIPT"
    chmod +x "$YZ_SCRIPT"
fi
YZLOG_SCRIPT="/usr/bin/yzlog"
if [ ! -f "$YZLOG_SCRIPT" ]; then
    echo '#!/bin/bash' > "$YZLOG_SCRIPT"
    echo 'echo 正在打开Yunzai-Bot后台日志' >> "$YZLOG_SCRIPT"
    echo "cd $LS_DIR" >> "$YZLOG_SCRIPT"
    echo 'pnpm run log' >> "$YZLOG_SCRIPT"
    chmod +x "$YZLOG_SCRIPT"
fi
YZLOGIN_SCRIPT="/usr/bin/yzlogin"
if [ ! -f "$YZLOGIN_SCRIPT" ]; then
    echo '#!/bin/bash' > "$YZLOGIN_SCRIPT"
    echo 'echo 正在打开Yunzai-Bot登录配置' >> "$YZLOGIN_SCRIPT"
    echo "cd $LS_DIR" >> "$YZLOGIN_SCRIPT"
    echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$YZLOGIN_SCRIPT"
    echo 'pnpm run login' >> "$YZLOGIN_SCRIPT"
    chmod +x "$YZLOGIN_SCRIPT"
fi
YZSTOP_SCRIPT="/usr/bin/yzstop"
if [ ! -f "$YZSTOP_SCRIPT" ]; then
    echo '#!/bin/bash' > "$YZSTOP_SCRIPT"
    echo 'echo 正在停止Yunzai-Bot运行' >> "$YZSTOP_SCRIPT"
    echo "cd $LS_DIR" >> "$YZSTOP_SCRIPT"
    echo 'pnpm stop' >> "$YZSTOP_SCRIPT"
    chmod +x "$YZSTOP_SCRIPT"
fi
XRK_SCRIPT="/usr/bin/xrk"
if [ ! -f "$XRK_SCRIPT" ]; then
    echo '#!/bin/bash' > "$XRK_SCRIPT"
    echo 'echo 正在打开向日葵脚本' >> "$XRK_SCRIPT"
    echo "bash <(curl -sL gitee.com/xrk114514/shell-Yunzai/raw/master/shell-all/ybjb.sh)" >> "$XRK_SCRIPT"
    chmod +x "$XRK_SCRIPT"
fi
ML_FILE=".ml"
COMMAND_LIST=(
    "笨比启动云崽命令为 yz"
    "后台查看日志命令为 yzlog"
    "重新配置账号命令为 yzlogin"
    "停止云崽运行命令为 yzstop"
    "启动向日葵脚本命令为 xrk"
)

if [ ! -f $ML_FILE ]; then
    for cmd in "${COMMAND_LIST[@]}" ; do
        key=${cmd% *}
        value=${cmd##* }
        echo "$key $value" >> $ML_FILE
    done
fi

echo "启动命令写入成功"
